package AgentTravel;

import java.util.HashMap;
import java.util.Map;


import AgentTravel.Behaviours.HostReceiveMessageBehaviour;
import AgentTravel.Behaviours.ServerAnswerQuestions;
import AgentTravel.Behaviours.ServerGetLoginBehaviour;
import AgentTravel.Behaviours.ServerGetModyficationsBehaviour;
import AgentTravel.Behaviours.ServerGetRegistrationBehaviour;
import AgentTravel.Behaviours.ServerGetUnsubscribeBehaviour;
import AgentTravel.LogicObjects.User;
import AgentTravel.LogicObjects.UserInfoForServer;
import AgentTravel.Messages.ServerConfirmationInfo;
import AgentTravel.gui.GuiAgent;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;

public class ServerAgent extends Agent 
{
	private Map<String,UserInfoForServer> users=new HashMap<String,UserInfoForServer>();
	private Map<String,String> passLogin=new HashMap<String,String>();
	private Map<String,AID> userGui=new HashMap<String,AID>();
	
	
	
	protected void setup() 
	{
		// Printout a welcome message
		System.out.println("Hello! Server-agent "+getAID().getName()+" is ready.");
		
		DFAgentDescription dfd=new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd=new ServiceDescription();
		sd.setType(MyOntology.serverService);
		sd.setName("default_server_name");
		sd.setOwnership(getAID().toString());
		dfd.addServices(sd);
		
		try
		{
			DFService.register(this, dfd);
			System.out.println("Server zapisany do yellowpages");
		}
		catch(FIPAException fe)
		{
			System.out.println("Blad przy rejestracji do yellowPages");
			fe.printStackTrace();
		}
		
		addBehaviour(new ServerGetRegistrationBehaviour(users, passLogin,userGui));
		addBehaviour(new ServerGetLoginBehaviour(users,passLogin,userGui));
		addBehaviour(new ServerGetModyficationsBehaviour(users, passLogin,userGui));
		addBehaviour(new ServerGetUnsubscribeBehaviour(users,passLogin,userGui));
		addBehaviour(new ServerAnswerQuestions(users, passLogin,userGui));
	}
	
	protected void takeDown()
	{
		try
		{
			DFService.deregister(this);
			System.out.println("Server wyrejestrowany z yellowPages");
		}
		catch (FIPAException fe)
		{
			System.out.println("Blad przy derejestracji z yellowPages");
		}
		
		System.out.println("Server-agent "+getAID().getName()+ " terminating.");
	}
}
